import Vue from 'vue'
import Vuex from 'vuex'
import { getUserInfoApi } from '@/utils/api'
// 本地持久化存储插件
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 令牌
    token: null,
    userInfo: null
  },
  getters: {
    nickname: (state) => state.userInfo.nickname,
    username: (state) => state.userInfo.username,
    user_pic: (state) => state.userInfo.user_pic
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async getUserInfoActions(store) {
      const { data: res } = await getUserInfoApi()
      if (res.code === 0) {
        store.commit('setUserInfo', res.data)
      }
    }
  },

  modules: {},
  // 配置vuex插件
  plugins: [createPersistedState()]
})
